<?php
/**
 * 实用类
 *
 * @author 孙晓晔
 * @version $Id: PortalUtil.class.php 254 2009-09-22 07:24:52Z sunxy $
 */

require_once(APP_LIB . 'Util.class.php');

class PortalUtil extends Util {
	
	public $nav = array(
		'home'=>false,
		'product'=>false,
		'service'=>false,
		'equipment'=>false,
		'contact'=>false,
	);

	function __construct($request) {
		parent::__construct($request);
		
		$this->dsn_r = 'wifi';
		$this->dsn_w = 'wifi';
	}

	/**
	 * 预处理
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		// parent::pretreat($controller);

		$root_url = array(
			'cn'=>'http://w2.wintec-machining.com/b/test/',
			'en'=>'http://www.wintec-machining.com/b/test/'
		);
		
		$controller->assign_by_ref('root_url', $root_url);

		$controller->assign_by_ref('nav', $this->nav);
		
		global $cfg;
		
		$controller->assign_by_ref('wlan', $cfg['wlan']['root']);
	}
	
	function query($controller, $request) {
		$p2 = '';
		// reset($_REQUEST);
		while (list($k, $v) = each($_POST)) {
			if (empty($v) || $v == '-') {
				// if (strlen($v) == 0 || $v == '-') {
				continue;
			}
	
			if ($this->not_query($k)) {
				continue;
			}
				
			$tmp = '';
			if (is_array($v)) {
				while (list($a, $b) = each($v)) {
					$tmp .= sprintf("&%s[]=%s", $k, $b);
				}
				$p2 .= $tmp;
			} else {
				$p2 .= '&' . $k . '=' . $v;
			}
			$controller->assign($k, $v);
		}
	
		while (list($k, $v) = each($_GET)) {
			if (empty($v) || $v == '-') {
				// if (strlen($v) == 0 || $v == '-') {
				continue;
			}
	
			if ($this->not_query($k)) {
				continue;
			}
	
			$tmp = '';
			if (is_array($v)) {
				while (list($a, $b) = each($v)) {
					$tmp .= sprintf("&%s[]=%s", $k, $b);
				}
				$p2 .= $tmp;
			} else {
				$p2 .= '&' . $k . '=' . $v;
			}
				
			$controller->assign($k, $v);
		}
	
		$controller->assign('p2', $p2);
	
		return $p2;
	}
	
	/**
	 * 判断是否是查询选项
	 *
	 * @param string $s
	 * @return boolean
	 */
	function not_query($s) {
		if (empty($s)) {
			return true;
		}
	
		if (substr($s, -2) == '_s') {
			return false;
		}
	
		$kk = array('id', 'status', 'code', 'r0', 'r1', 'r2');
		if (in_array($s, $kk)) {
			return false;
		}
	
		return true;
	}
	
	function _orderby($default = "id") {
		if (empty($this->orderby_s) || $this->orderby_s == '-') {
			$orderby = $default;
		}
	
		if ($this->orderby_s == '1') {
			$orderby = "id";
		}
	
		if ($this->orderby_s == '2') {
			$orderby = "modifytime";
		}
	
		if (empty($orderby)) {
			$orderby = $this->orderby_s;
		}
	
		$fmt = " ORDER BY %s %s";
		$ret = sprintf($fmt, $orderby, $this->desc);
	
		return $ret;
	}
	
	function findPara($controller, $request) {
		$p2 = $this->query($controller, $request);
	
		if (!empty($request['p'])) {
			$p2 .= '&p=' . $request['p'];
			$controller->assign('p', $request['p']);
		}
		$controller->assign('p3', $p2);
	
		$this->orderby_s = $request['orderby_s'];
		$this->desc_s = $request['desc_s'];
	
		if ($this->desc_s == '1') {
			$this->desc = "";
		} else {
			$this->desc = " DESC";
		}
	}

	function _input($s) {
		if (empty($s)) {
			return '';
		}

		$a = preg_split("/[\s,|:|\\,]/i", $s);
		if (count($a) > 1) {
			return $a[0];
		}

		return $s;
	}
	
	function _view($id = 0, $row = null) {
	
		return $row;
	}
	
	function _view_t($row) {
		
	
		if (!empty($row['status'])) {
			$row['status_t'] = $this->status[$row['status']];
		}
	
		return $row;
	}
	
	function _mac_x($mac) {
		
		if (empty($mac)) {
			return '';
		}
	
		$ret = Common::x(Constant::tbl_wifi_mac, 'mac', $mac);

		return $ret;
	}
	
	function _mac_t($mac) {
		
		if ($ret = $this->_mac_x($mac)) {
			return $ret['name'];
		}
		
		return '';
	}
	
	function _p_list() {
		require_once 'Product.class.php';
		
		$product = new Product();
		return $product->_list();
	}
	
	function _wifidog($request) {
		
		if (!empty($request['gw_address'])) {
			$_SESSION['wifidog'] = json_encode($request);
		}
	}
	
	function _dog_address($request) {
		$gw_address = $request['gw_address'];
		if (!empty($gw_address)) {
			return $gw_address;
		}
		
		$dog = json_decode($_SESSION['wifidog']);
		
		return $dog->gw_address;
	}
	
	function _dog_port($request) {
		$gw_port = $request['gw_port'];
		
		if (!empty($gw_port)) {
			return $gw_port;
		}
		
		$dog = json_decode($_SESSION['wifidog']);
		
		return $dog->gw_port;
	}
	
	function _dog_id($request) {
		$gw_id = $request['gw_id'];
		
		if (!empty($gw_id)) {
			return $gw_id;
		}
		
		$dog = json_decode($_SESSION['wifidog']);
		
		return $dog->gw_id;
	}
	
	function _dog_mac($request) {
		$mac = $request['mac'];
		
		if (!empty($mac)) {
			return $mac;
		}
		
		$dog = json_decode($_SESSION['wifidog']);
		
		return $dog->mac;
	}
	
	function _dog_url($request) {
		$url = $request['url'];
		
		if (!empty($url)) {
			return $url;
		}
		
		$dog = json_decode($_SESSION['wifidog']);
		
		return $dog->url;
	}
	
	function http_post_data($url, $data_string) {
	
		$header = array(
			'Content-Type: application/json; charset=utf-8',
			'Content-Length: ' . strlen($data_string)
		);
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	
		ob_start();
		curl_exec($ch);
		$content = ob_get_contents();
		ob_end_clean();
	
		$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$ret = array($code, $content);
	
		return $ret;
	}
}
?>